<template>
  <div class="login">
    <div class="login-container">
      <div class="avatar">
        <img src="../../assets/logo.png" alt="" />
      </div>
      <el-form :model="loginForm" status-icon :rules="rules" ref="formRef" label-width="70px" class="demo-loginForm">
        <el-form-item label="用户名" prop="username">
          <el-input prefix-icon="el-icon-user" v-model="loginForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input prefix-icon="el-icon-lock" type="password" v-model="loginForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item class="form_btn">
          <el-button type="info" @click="resetForm">重置</el-button>
          <el-button type="primary" @click="loginIn">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      rules: {
        username: [
          { min: 5, max: 12, message: '请输入5-12位用户名', trigger: 'blur' },
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { min: 6, max: 16, message: '请输入6-16位密码', trigger: 'blur' },
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    resetForm() {
      this.$refs.formRef.resetFields()
    },
    loginIn() {
      this.$store
        .dispatch('loginIn', { ...this.loginForm })
        .then((res) => {
          this.$router.replace('/home')
          this.$message.success(res.msg)
        })
        .catch((err) => {
          this.$message.error(err.msg)
        })
    },
  },
}
</script>

<style lang="less" scoped>
div.login {
  position: relative;
  background: #c1cdcd;
  height: 100%;
  width: 100%;
  div.login-container {
    position: absolute;
    top: 50%;
    left: 50%;
    height: 350px;
    width: 450px;
    background: #fff;
    transform: translate(-50%, -50%);
    border-radius: 5px;
    div.avatar {
      height: 100px;
      width: 100px;
      padding: 5px;
      border: 1px solid #fff;
      position: absolute;
      top: -50px;
      left: 50%;
      transform: translateX(-50%);
      border-radius: 50%;
      background: #fff;
      img {
        height: 100px;
        width: 100px;
        border-radius: 50%;
        background: #eed;
      }
    }
    .el-form.demo-loginForm {
      position: absolute;
      bottom: 0px;
      padding: 20px 40px;
      width: 100%;
      box-sizing: border-box;
      .el-form-item.form_btn {
        display: flex;
        justify-content: flex-end;
        .el-button {
          margin-left: 30px;
          width: 80px;
        }
      }
    }
  }
}
</style>
